﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Aioms.Dtos;
using Aioms.Models;
using Furion.DynamicApiController;
using Furion.LinqBuilder;
using Furion.UnifyResult;
using Mapster;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using SqlSugar;

namespace Aioms.Controllers
{
    /// <summary>
    /// 巡检点巡检项结果上传
    /// </summary>
    [DynamicApiController]
    public class DeviceStandardResultController
    {
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public DeviceStandardResultController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
        }

        /// <summary>
        /// 设备巡检/点检标准结果上传
        /// </summary>
        /// <param name="dsr"></param>
        /// <returns></returns>
        public async Task<IActionResult> PostUploadresult([FromForm] DeviceStandardResultDto dsr)
        {
            await db.Insertable<DeviceStandardResult>(dsr.Adapt<DeviceStandardResult>()).ExecuteCommandAsync();
            await db.Updateable<TpWorkOrder>(new TpWorkOrder { Progress = dsr.Progress })
                .UpdateColumns(wo => new { wo.Progress })
                .Where(wo => wo.Eid == dsr.Eid && wo.Id == dsr.Woid).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }
    }
}
